﻿#region using

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

#endregion

namespace MsBuildAccess.Helper
{
    /// <summary>
    /// Log helper
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Task logging helper
        /// </summary>
        private readonly TaskLoggingHelper log;

        /// <summary>
        /// Log target of the logger
        /// </summary>
        private readonly string logTarget;

        /// <summary>
        /// Initializes a new instance of the Logger class.
        /// </summary>
        /// <param name="logTarget">logtarget normal or console</param>
        /// <param name="log">TaskLoggingHelper class for logging</param>
        public Logger(string logTarget, TaskLoggingHelper log)
        {
            this.logTarget = logTarget;
            this.log = log;
        }

        #region logging

        /// <summary>
        /// internal logging
        /// </summary>
        /// <param name="message">messege for log</param>
        /// <param name="isError">is error message</param>
        public void LogInternal(string message, bool isError)
        {
            if (isError)
            {
                this.LogErr(message);
            }
            else
            {
                this.LogMsg(message);
            }
        }

        /// <summary>
        /// log error message
        /// </summary>
        /// <param name="message">messege for log</param>
        public void LogErr(string message)
        {
            if (this.logTarget.Trim() == "console")
            {
                Console.WriteLine(message);
            }
            else
            {
                this.log.LogError(message);
            }
        }

        /// <summary>
        /// Log normal message
        /// </summary>
        /// <param name="message">messege for log</param>
        public void LogMsg(string message)
        {
            if (this.logTarget.Trim() == "console")
            {
                Console.WriteLine(message);
            }
            else
            {
                this.log.LogMessage(MessageImportance.Normal, message);
            }
        }

        #endregion
    }
}